package me.may.money.command;

import java.util.Map;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.google.common.collect.Maps;

import me.may.money.Entry;
import me.may.money.api.MoneyAPI;
import me.may.money.command.subcommand.AddCommand;
import me.may.money.command.subcommand.AdminCommand;
import me.may.money.command.subcommand.LookCommand;
import me.may.money.command.subcommand.ReloadCommand;
import me.may.money.command.subcommand.SetCommand;
import me.may.money.command.subcommand.TakeCommand;
import me.may.money.command.subcommand.TopCommand;
import me.may.money.util.SubCommand;

public class MoneyCommandHandler implements CommandExecutor {

	private Map<String, SubCommand> subCommandMap = Maps.newHashMap();
	
	public MoneyCommandHandler() {
		registerSubCommand("admin", new AdminCommand());
		registerSubCommand("look", new LookCommand());
		registerSubCommand("add", new AddCommand());
		registerSubCommand("take", new TakeCommand());
		registerSubCommand("set", new SetCommand());
		registerSubCommand("top", new TopCommand());
		registerSubCommand("reload", new ReloadCommand());
	}
	
	public void registerSubCommand(String commandName, SubCommand command) {
		if (subCommandMap.containsKey(commandName)) {
			Entry.getInstance().getLogger().warning("发现重复子命令注册!");
		}
		subCommandMap.put(commandName, command);
	}
	
	public void unregisterSubCommand(String commandName) {
		subCommandMap.remove(commandName);
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		if(cmd.getName().equalsIgnoreCase("money")) {
			if(args.length == 0) {
				if(!(sender instanceof Player)) {
					subCommandMap.get("admin").execute(sender, args);
					return true;
				}
				sender.sendMessage("§8[§6交子§8] §e>> §7你拥有交子§a " + MoneyAPI.getPlayerMoney(sender.getName()) + " §7两");
				return true;
			}
			
			SubCommand subCommand = subCommandMap.get(args[0]);
			if (subCommand == null) {
				sender.sendMessage("§8[§6交子§8] §e>> §c未知指令!");
				return true;
			}
			subCommand.execute(sender, args);
		}
		return false;
	}

}
